package cn.bjc.system.controller;

import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.PageList;
import cn.bjc.system.domain.Permission;
import cn.bjc.system.query.PermissionQuery;
import cn.bjc.system.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//系统-权限管理-接口
@RestController
@RequestMapping("/permission")
public class PermissionController {
    
    @Autowired
    private IPermissionService permissionService;

    
    //查询所有明细
    @GetMapping
    public List<Permission> findAll(){
        return permissionService.loadAll();
    }
    
    
    //通过id查询明细
    @GetMapping("/{id}")
    public Permission findOne(@PathVariable("id") Long id){
        return permissionService.loadById(id);
    }
    
    //添加和修改
    @PutMapping()
    public JsonResult updateOrAdd(@RequestBody Permission permission){
        try {
            if(permission.getId()==null){
                permissionService.save(permission);
            }else {
                permissionService.update(permission);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败,稍后重试");
        }
    }
    
    //通过id删除
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            permissionService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败,稍后重试");
        }
    }
    
    //分页查询
    @PostMapping
    public PageList<Permission> queryPage(@RequestBody PermissionQuery query){
        try {
            return permissionService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<Permission>();
        }
    }
    
    //批量删除
    @PatchMapping()
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            permissionService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }
    
    
}
